const jwt = require("jsonwebtoken")
const db = require("../../json-server/db")

const router = new require("@koa/router")()
router.prefix("/user")

router.post("/login", async (ctx, next) => {
  const { username, password } = ctx.request.body
  const isPass = await db.checkUser(username, password)
  if (isPass) {
    let token = jwt.sign({ username }, "2302B")
    ctx.body = {
      success: true,
      msg: "登录成功",
      data: {
        token,
      },
    }
  } else {
    ctx.body = {
      success: false,
      data: {},
    }
  }
})

router.post("/register", async (ctx, next) => {
  // 接口处理的代码
})

module.exports = router
